package dataview

import (
	"fmt"
	"go-iot-gateway/internal/conf_table"
	sys_contant "go-iot-gateway/internal/sys/contant"
	sys_runtime "go-iot-gateway/internal/sys/runtime"
	"sort"
	"strings"
	"time"
)

func DataViewIndex() string {
	channelList, transferChannelList := channelList()
	sub := time.Now().Sub(sys_runtime.StartingTime())

	html := strings.ReplaceAll(indexHtmlTemp, "${uptime}", fmt.Sprintf("%s", sub))
	html = strings.ReplaceAll(html, "${version}", sys_contant.APP_VERSION)
	html = strings.ReplaceAll(html, "${channelList}", channelList)
	html = strings.ReplaceAll(html, "${transferChannelList}", transferChannelList)
	return html
}

func channelList() (string, string) {
	resultA := ""
	resultB := ""

	channelList := conf_table.FindAllChannelList()
	sort.Slice(channelList, func(i, j int) bool {
		return channelList[i].Id < channelList[j].Id
	})

	for _, chl := range channelList {
		if !chl.IsValid() || chl.Chl_name == "sys_channel" {
			continue
		}
		if chl.IsTransfer() {
			resultB += fmt.Sprintf("<li><b>%s</b><ul>", chl.Chl_name)
		} else {
			resultA += fmt.Sprintf("<li><b>%s</b><ul>", chl.Chl_name)
		}

		devices := conf_table.FindAllDeviceByChlId(chl.Chl_id)
		if len(devices) <= 0 {
			continue
		}
		sort.Slice(devices, func(i, j int) bool {
			return devices[i].Id < devices[j].Id
		})

		for _, dev := range devices {
			if !dev.IsValid() {
				continue
			}
			if chl.IsTransfer() {
				resultB += fmt.Sprintf("<li><a href=\"dataview/device/%s\" target=\"icontent\">%s</a></li>", dev.Dev_id, dev.Dev_name)
			} else {
				resultA += fmt.Sprintf("<li><a href=\"dataview/device/%s\" target=\"icontent\">%s</a></li>", dev.Dev_id, dev.Dev_name)
			}
		}
		if chl.IsTransfer() {
			resultB += "</ul></li>"
		} else {
			resultA += "</ul></li>"
		}
	}

	return resultA, resultB
}

var indexHtmlTemp = `
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>GO-IOT-GATEWAY</title>
    <style>
        html,body{
            padding: 0;
            margin: 0;
            height: 100%;
        }
        div{
            padding: 0;
            margin: 0;
        }
        .container {
            width: 100%;
            height: 100%;
        }
        .left {
            width: 230px;
            height: 100%;
            text-align: center;
            padding: 20px 0;
            background: rgba(208, 198, 198, 0.27);
            float: left;
        }
        .right {
            /*margin: 2px;*/
            height: 100%;
            overflow: hidden; /* 触发bfc */
            /*background: green;*/
        }
        li{
            text-align: left;
        }
		ul{
		  	padding-inline-start: 20px;
		}
		.header{
          display: inline-block;
		  width: 100%;
          background: rgba(208, 198, 198, 0.27);
          padding: 1px 10px
		}
    </style>
</head>
<body>
<div class="header">版本: ${version} | 运行时间:${uptime}</div>
<div class="container">
    <div class="left">
        <ul>
          ${channelList}   
        </ul>
		<hr>
		<ul>
          ${transferChannelList}   
        </ul>
    </div>
    <div class="right">
        <iframe id="icontent" name="icontent" width="100%" height="100%"   src="" ></iframe>
    </div>
</div>
</body>
</html>
`
